<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规范号" prop="gaugeNumber">
        <el-input
          v-model="queryParams.gaugeNumber"
          placeholder="请输入规范号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质1" prop="material1">
        <el-input
          v-model="queryParams.material1"
          placeholder="请输入材质1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质1厚度(mm)" prop="materialThickness1">
        <el-input
          v-model="queryParams.materialThickness1"
          placeholder="请输入材质1厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涂胶1" prop="gumming1">
        <el-input
          v-model="queryParams.gumming1"
          placeholder="请输入涂胶1"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质2" prop="material2">
        <el-input
          v-model="queryParams.material2"
          placeholder="请输入材质2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质2厚度(mm)" prop="materialThickness2">
        <el-input
          v-model="queryParams.materialThickness2"
          placeholder="请输入材质2厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涂胶2" prop="gumming2">
        <el-input
          v-model="queryParams.gumming2"
          placeholder="请输入涂胶2"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质3" prop="material3">
        <el-input
          v-model="queryParams.material3"
          placeholder="请输入材质3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质3厚度(mm)" prop="materialThickness3">
        <el-input
          v-model="queryParams.materialThickness3"
          placeholder="请输入材质3厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="涂胶3" prop="gumming3">
        <el-input
          v-model="queryParams.gumming3"
          placeholder="请输入涂胶3"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质4" prop="material4">
        <el-input
          v-model="queryParams.material4"
          placeholder="请输入材质4"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="材质4厚度(mm)" prop="materialThickness4">
        <el-input
          v-model="queryParams.materialThickness4"
          placeholder="请输入材质4厚度(mm)"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['LapJoint:lapJoint:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['LapJoint:lapJoint:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['LapJoint:lapJoint:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClear"
          v-hasPermi="['LapJoint:lapJoint:clear']"
        >清空数据</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-upload"
          size="mini"
          @click="handleImport"
          v-hasPermi="['LapJoint:lapJoint:import']"
        >导入</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['LapJoint:lapJoint:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="lapJointList"
      @selection-change="handleSelectionChange"
      border
      style="width: 100%;"
      :show-overflow-tooltip="true"
      empty-text="暂无数据"
    >

    <el-table-column type="selection" align="center" width="55"></el-table-column>
    <el-table-column label="序号" type="index" align="center">
      <template slot-scope="scope">
        <span>{{(queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1}}</span>
      </template>
    </el-table-column>
      <el-table-column label="规范号" align="center" prop="gaugeNumber" />
      <el-table-column label="材质1" align="center" prop="material1" />
      <el-table-column label="材质1厚度(mm)" align="center" prop="materialThickness1" />
      <el-table-column label="涂胶1" align="center" prop="gumming1" />
      <el-table-column label="材质2" align="center" prop="material2" />
      <el-table-column label="材质2厚度(mm)" align="center" prop="materialThickness2" />
      <el-table-column label="涂胶2" align="center" prop="gumming2" />
      <el-table-column label="材质3" align="center" prop="material3" />
      <el-table-column label="材质3厚度(mm)" align="center" prop="materialThickness3" />
      <el-table-column label="涂胶3" align="center" prop="gumming3" />
      <el-table-column label="材质4" align="center" prop="material4" />
      <el-table-column label="材质4厚度(mm)" align="center" prop="materialThickness4" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['LapJoint:lapJoint:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['LapJoint:lapJoint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!-- 添加或修改【请填写功能名称】对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规范号" prop="gaugeNumber">
          <el-input v-model="form.gaugeNumber" placeholder="请输入规范号" />
        </el-form-item>
        <el-form-item label="材质1" prop="material1">
          <el-input v-model="form.material1" placeholder="请输入材质1" />
        </el-form-item>
        <el-form-item label="材质1厚度(mm)" prop="materialThickness1">
          <el-input v-model="form.materialThickness1" placeholder="请输入材质1厚度(mm)" />
        </el-form-item>
        <el-form-item label="涂胶1" prop="gumming1">
          <el-input v-model="form.gumming1" placeholder="请输入涂胶1" />
        </el-form-item>
        <el-form-item label="材质2" prop="material2">
          <el-input v-model="form.material2" placeholder="请输入材质2" />
        </el-form-item>
        <el-form-item label="材质2厚度(mm)" prop="materialThickness2">
          <el-input v-model="form.materialThickness2" placeholder="请输入材质2厚度(mm)" />
        </el-form-item>
        <el-form-item label="涂胶2" prop="gumming2">
          <el-input v-model="form.gumming2" placeholder="请输入涂胶2" />
        </el-form-item>
        <el-form-item label="材质3" prop="material3">
          <el-input v-model="form.material3" placeholder="请输入材质3" />
        </el-form-item>
        <el-form-item label="材质3厚度(mm)" prop="materialThickness3">
          <el-input v-model="form.materialThickness3" placeholder="请输入材质3厚度(mm)" />
        </el-form-item>
        <el-form-item label="涂胶3" prop="gumming3">
          <el-input v-model="form.gumming3" placeholder="请输入涂胶3" />
        </el-form-item>
        <el-form-item label="材质4" prop="material4">
          <el-input v-model="form.material4" placeholder="请输入材质4" />
        </el-form-item>
        <el-form-item label="材质4厚度(mm)" prop="materialThickness4">
          <el-input v-model="form.materialThickness4" placeholder="请输入材质4厚度(mm)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip text-center" slot="tip">
          <div class="el-upload__tip" slot="tip">
            <el-checkbox v-model="upload.updateSupport" /> 是否更新已经存在的用户数据
          </div>
          <span>仅允许导入xls、xlsx格式文件。</span>
          <el-link type="primary" :underline="false" style="font-size:12px;vertical-align: baseline;" @click="importTemplate">下载模板</el-link>
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="Export.title" :visible.sync="Export.open" width="400px" append-to-body>
      <span>是否导出全部数据</span>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="ExportAll">确 定</el-button>
        <el-button @click="Export.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLapJoint, getLapJoint, delLapJoint, addLapJoint, updateLapJoint } from "@/api/WELD/lapJoint";
import { getToken } from "@/utils/auth";
import axios from "axios";

export default {
  name: "WELD",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 唯一标识符
      uniqueId: "",
      // 选中数组
      ids: [],
      // 菜单列表
      menuOptions: [],
      // 存储用户选择的数据项
      multipleSelection: [],
      // 选中表数组
      tableNames: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch:false,
      //导入文件的名称
      importFileName: null,
      // 总条数
      total: 0,
      // 表数据
      lapJointList: [],
      // 日期范围
      dateRange: "",
      // 部门树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},

      defaultProps: {
        children: "children",
        label: "label"
      },

      // 用户导入参数
      upload:{
        // // 是否显示弹出层（用户导入）
        open: false,
        // // 弹出层标题（用户导入）
        title: "",
        // // 是否禁用上传
        isUploading: false,
        // // 是否更新已经存在的用户数据
        updateSupport: 0,
        // // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/LapJoint/lapJoint/importData"
      },

      // 导出参数
      Export:{
        // // 是否显示弹出层
        open: false,
        // // 弹出层标题
        title: "",
      },

      queryParams: {
        pageNum: 1,
        pageSize: 50,
        ipaddr: undefined,
        name:undefined,
        tableName: undefined,
        tableComment: '' // 假设这是下拉列表绑定的模型
      },
    };
  },
  created() {
    // 从 localStorage 中恢复 importFileName
    const storedFileName = localStorage.getItem('importFileName');
    if (storedFileName) {
      this.importFileName = storedFileName;
    }
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      listLapJoint(this.queryParams).then(response => {
        this.lapJointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ID: undefined,
        sjNumber: undefined,
        robot: undefined,
        gaugeNumber: undefined,
        position: undefined,
        electrodeForm: undefined,
        material1: undefined,
        materialThickness1: undefined,
        gumming1: undefined,
        material2: undefined,
        materialThickness2: undefined,
        gumming2: undefined,
        material3: undefined,
        materialThickness3: undefined,
        gumming3: undefined,
        material4: undefined,
        materialThickness4: undefined,
        stress: undefined,
        preloadingTime: undefined,
        preheatingTime: undefined,
        preheatingCurrent: undefined,
        coolingTime: undefined,
        incrementalTime: undefined,
        startingCurrent: undefined,
        lapJointingTime: undefined,
        lapJointingCurrent: undefined,
        coolingTime1: undefined,
        declineTime: undefined,
        terminationCurrent: undefined,
        pulseFrequency: undefined,
        coolingTime2: undefined,
        temperingTime: undefined,
        temperingCurrent: undefined,
        holdTime: undefined,
        parametricForm: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
      this.multipleSelection =selection;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加【请填写功能名称】";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ID = row.ID || this.ids
      getLapJoint(ID).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改【请填写功能名称】";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateLapJoint(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLapJoint(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const Ids = row.ID || this.ids;
      let displayIds=Ids;
      if (Ids.length>10){
        displayIds=Ids.slice(0,10)+"...";
      }
      this.$modal.confirm('是否确认删除编号为"' + displayIds + '"的数据项？').then(function() {
        return delLapJoint(Ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 清空按钮操作 */
    handleClear(){
      this.$modal.confirm('是否确认清空所有数据项？').then(() =>{
        axios.delete('/dev-api/LapJoint/lapJoint/clear',{
          headers: {
            // 将 token 添加到请求头中，通常是 'Authorization' 头
            'Authorization': `Bearer ${getToken()}`
          }
        }).then(response =>{
          this.$modal.msgSuccess("清空成功！");
          this.getList();
        }).catch(err=>{
          this.$modal.msgError("清空失败：" + error.message);
        });
      }).catch(() => {});
    },

    /** 导出按钮操作 */
    handleExport() {
      if (this.multipleSelection.length===0){
        this.Export.title = "数据导入";
        this.Export.open = true;
      }else {
        const ids = this.multipleSelection.map(item => item.id).join(',');
        this.download('LapJoint/lapJoint/export', {ids}, `${this.importFileName}`)
      }
    },
    ExportAll(){
      this.download('LapJoint/lapJoint/exportAll',1,`${this.importFileName}`);
      this.Export.open = false;
    },

    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = "数据导入";
      this.upload.open = true;
    },
    /** 下载模板操作 */
    importTemplate() {
      this.download('LapJoint/lapJoint/importTemplate', {
      }, `user_template_${new Date().getTime()}.xlsx`)
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      // 保存导入文件的名称
      this.importFileName = file.name;
      // 保存到 localStorage
      localStorage.setItem('importFileName', this.importFileName);
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert("<div style='overflow: auto;overflow-x: hidden;max-height: 70vh;padding: 10px 20px 0;'>" + response.msg + "</div>", "导入结果", { dangerouslyUseHTMLString: true });
      this.getList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  }
};
</script>
